/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dancrawler.client;

import java.io.File;
import dancrawler.common.Misc;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @author Team 4 RMI, HINGDA10
 */
public class Installer {

  private InputStream fileFrom;
  private String fileTo;
  private String path;
  private String[][] fileList = {{"dancrawler.s3db", "DB"}, {"jogg-0.0.7.jar", "jar"}, {"jorbis-0.0.15.jar", "jar"},
    {"libsqlite4java-android-armv7.so", "droid"}, {"libsqlite4java-linux-amd64.so", "la64"},
    {"libsqlite4java-linux-i386.so", "l86"}, {"libsqlite4java-osx-10.4.jnilib", "osx10"},
    {"libsqlite4java-osx-ppc.jnilib", "ppc"}, {"libsqlite4java-osx.jnilib", "osx"},
    {"sqlite4java-win32-x64.dll", "w64"}, {"sqlite4java-win32-x86.dll", "w86"},
    {"sqlite4java.jar", "jar"}, {"tritonus_share.jar", "jar"}, {"vorbisspi1.0.3.jar", "jar"}};
  private String[] soundList = {"BigBossDeath.wav", "BossDeath.wav", "Death.wav", "DownStairs8-Bit.wav", "FlameMagic.wav", "FreezeMagic.wav", "Heal8-Bit.wav", "LRWeird 1 by Lionel Allorge.wav", "LRWeird 2 by Lionel Allorge.wav", "Oddbounce.wav", "Powerup.wav", "StormMagic.wav", "UpStairs8-Bit.wav"};
  private static String OS = null;
  private File source;
  private File dest;
  private final String datafolder = "dancrawler";
  private String[] pathDirs;
  private String systemPath;
  private String targetPathOpen;
  private String targetPathClosed;

  Installer() {
    path = Misc.getAppdataPath("");
    targetPathOpen = Misc.getAppdataPath(File.separatorChar + datafolder);
    targetPathClosed = Misc.getAppdataPath(File.separatorChar + datafolder + File.separatorChar);
  }

  public String PathExists() {
    systemPath = System.getenv("PATH");
    pathDirs = systemPath.split(File.pathSeparator);
    for (String p : pathDirs) {
      if (p.startsWith(targetPathOpen));
      return null;
    }
    return "Set '" + targetPathOpen + "' in your System Path variables. Then restart.";
  }

  public String makeDataFolder() {
    try {
      File f = new File(targetPathOpen);
      f.mkdir();
      System.out.println(f.getAbsolutePath());
      return null;
    } catch (Exception e) {
    }
    return "Unable to create data folder at " + targetPathOpen;
  }

  public void copyMusic() throws FileNotFoundException, IOException {
    for (int i = 0; i < soundList.length; i++) {
      try {
        fileFrom = getClass().getResourceAsStream("/res/sounds/" + soundList[i]);
        System.out.println(fileFrom);
      } catch (Exception e) {
        System.out.println("Cannot read files!" + e);
      }
      fileTo = targetPathClosed + File.separatorChar + soundList[i].toString();

      dest = new File(fileTo);
      if (!dest.exists()) {
        try {
          dest.createNewFile();
          OutputStream resStreamOut = new FileOutputStream(dest);
          int readBytes;
          byte[] buffer = new byte[4096];
          while ((readBytes = fileFrom.read(buffer)) > 0) {
            resStreamOut.write(buffer, 0, readBytes);
          }
          fileFrom.close();
          resStreamOut.close();
        } catch (Exception e) {
        }
      }
    }
  }

  public void copyFiles() throws FileNotFoundException, IOException {
    for (int i = 0; i < fileList.length; i++) {
      try {
        fileFrom = getClass().getResourceAsStream("/res/bin/" + fileList[i][0]);
      } catch (Exception e) {
      }
      fileTo = targetPathClosed + File.separatorChar + fileList[i][0];
      System.out.println(fileTo);
      dest = new File(fileTo);
      if (!dest.exists()) {
        try {
          dest.createNewFile();
          OutputStream resStreamOut = new FileOutputStream(dest);
          int readBytes;
          byte[] buffer = new byte[4096];
          while ((readBytes = fileFrom.read(buffer)) > 0) {
            resStreamOut.write(buffer, 0, readBytes);
          }
          fileFrom.close();
          resStreamOut.close();
        } catch (Exception e) {
        }
      }
    }
  }
}
